package com.wanjia.estate.constant.printinfo;

import lombok.AllArgsConstructor;

import java.util.Objects;
import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * 打印机错误类型
 * <p>-1 = OK</p>
 * <p>0 = service requested</p>
 * <p>1 = offline</p>
 * <p>2 = paper jammed</p>
 * <p>3 = door open</p>
 * <p>4 = no toner</p>
 * <p>5 = toner low</p>
 * <p>6 = out of paper</p>
 * <p>7 = low paper</p>
 */
@AllArgsConstructor
public enum ErrorType {

    OK(-1, "OK", null),
    SERVICE_REQUESTED(0, "service requested", null),
    OFFLINE(1, "offline", null),
    PAPER_JAMMED(2, "paper jammed", null),
    DOOR_OPEN(3, "door open", null),
    NO_TONER(4, "no toner", null),
    TONER_LOW(5, "toner low", null),
    OUT_OF_PAPER(6, "out of paper", null),
    LOW_PAPER(7, "low paper", null);


    private final Integer code;
    private final String statusName;
    private final String desc;

    public Integer code() {
        return code;
    }

    public String statusName() {
        return statusName;
    }

    public String desc() {
        return desc;
    }

    public static ErrorType get(Integer code) {
        return Stream.of(values())
                .filter(s -> Objects.equals(code, s.code))
                .findFirst()
                .orElse(null);
    }

    public static ErrorType getOrElse(Integer code, ErrorType orElse) {
        return Stream.of(values())
                .filter(s -> Objects.equals(code, s.code))
                .findFirst()
                .orElse(orElse);
    }

    public static ErrorType getOrElseGet(Integer code, Supplier<ErrorType> supplier) {
        return Stream.of(values())
                .filter(s -> Objects.equals(code, s.code))
                .findFirst()
                .orElseGet(supplier);
    }

    public static <X extends Throwable> ErrorType getOrElseThrow(Integer code, Supplier<? extends X> supplier) throws X {
        return Stream.of(values())
                .filter(s -> Objects.equals(code, s.code))
                .findFirst()
                .orElseThrow(supplier);
    }
}
